<?php
if(!defined('SYSTEM_PATH' ) ) die('Cannot access this page directly or see your configuration file ' ) ;

/**
* Controller
*
* @desc Allow to create actions
* 
* @package Cosmo
* 
* @author mentiss
* @version 1.0
* 
* @access public
*/
class Controller extends Cosmo{
    protected 	$template ;
    protected 	$lang 		= array() ;
    protected 	$_core 		= null ;
    private 	$_bHasTemplate = false ;

    /**
    * Controller::__construct()
    *
    * @desc Construction du controller
    */
    final public function __construct() {
		parent::__construct() ;
		$this->_core		= Core::getInstance() ;
		$this->template 	= $this->load->object('template') ; // get template object
		$this->lang 		= $this->load->language('controller') ; // get language
		$this->template->assign('lang', $this->lang) ;
		$this->template->setPath('all', strtolower(get_class($this))) ; // set default path to template for the controller
		if(count($this->dependencies ) > 0) {
			foreach($this->dependencies as $extend ){ // get all dependencies
		     	$this->{$extend} =& $this->load->object($extend) ;
		     	if($this->{$extend}->isError()) {
			 		$this->addLog('error', $this->{$extend}->getError()) ;
		          if($this->{$extend}->isStopped())
					$this->shutdown($this->{$extend}->getError()) ;
		     	}
			}
		}
		if(count($helpers ) > 0){ /* seems not to work ! maybe same for dependencies  Need to debug this */
		  foreach($helpers as $helper) { // get all helpers
		     	Loader::helper($helper) ;
			}
		}
		return true;
	}
    /**
    * Controller::shutdown()
    *
    * @desc Redéfinition de l'arrêt du système en cas de problème
    * @param string $msg
    * @return
    */
    public function shutdown($msg ) {
    	   $this->template->setPath('all','') ;
        $this->template->assign('error', $msg) ;
        $this->template->add('error/shutdown', true) ;
    }

    /**
    * Controller::_init()
    *
    * @desc Pseudo constructor of child controller
    */
    public function _init() {
    }
    
    /**
     * Controller::_execute()
     * 
     * @desc What to execute when arrived on controller. 
     * 
     * @return 
     */
    public function _map() {
		if(!is_null($this->_core->getContext('method')) && (method_exists(&$this, $this->_core->getContext('method')))) {			
			if(!is_null($this->_core->getContext('data'))) {
				$this->{$this->_core->getContext('method')}($this->_core->getContext('data')) ;
			} else {
				$this->{$this->_core->getContext('method')}() ;
			}
		} elseif(!is_null($this->_core->getContext('method'))) {
			$this->_core->setContext('method', 'index') ;
			if(method_exists($this, 'index'))
				$this->index() ;
			else
				$this->shutdown('There is not method index defined in this controller') ;
		} else {
			$this->template->assign('method', $this->_core->getContext('method')) ;
			$this->shutdown('Controller does not have the method ('.$this->_core->getContext('method').') you requested') ;
		}
    	}
    	final protected function _template_add($file, $stop = false, $type = 'template', $name = null) {
    		$bReturn = $this->template->add($file, $stop, $type, $name) ;
    		if($bReturn === true)
			$this->_bHasTemplate = true ;
			
		return $bReturn;
	}
    	public function _output() {
	    if($this->_bHasTemplate === false) {
	    		$this->template->add($this->_core->getContext('method')) ;
	    }
	}
}

?>